package api

import (
	"api-gateway/basic/config"
	__ "api-gateway/basic/proto"
	"api-gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 用户注册
func RegisterUser(c *gin.Context) {
	var req request.RegisterUser
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	user, err := config.UserClient.RegisterUser(c, &__.RegisterUserReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "用户注册失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "用户注册成功",
		"data": user.UserId,
	})
}

// 用户登录
func LoginUser(c *gin.Context) {
	var req request.LoginUser
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	user, err := config.UserClient.LoginUser(c, &__.LoginUserReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "用户登录失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "用户登录成功",
		"data": user.UserId,
	})

}
